<template>
	<div>
		<div style="float: right;margin-right:2%;">
			<el-button style='margin: 10px 0;' size="mini" type="primary" icon="document" @click="Notice">刷新</el-button>
			<el-button style='margin: 10px 0;' :disabled="!isAuth('creditScore:add')" size="mini" type="primary"
				icon="document" @click="addNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="tableData.records">
			<el-table-column fixed prop="rewardId" label="编号" width="50">
			</el-table-column>
			<el-table-column prop="levelName" label="名称">
			</el-table-column>
			<el-table-column prop="iconImg" label="图标">
			  <template slot-scope="scope">
			    　　<img :src="scope.row.iconImg" width="60" height="60" />
			  </template>
			</el-table-column>
			<el-table-column prop="scale" label="分佣比例">
			</el-table-column>
			<el-table-column prop="orderCount" label="完成单数">
			</el-table-column>
			<el-table-column prop="isEnable" label="状态">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.isEnable"
						@change="change(scope.row)"
						:disabled="!isAuth('universalMission:update')" :active-value="openValue"
						:inactive-value="closeValue" active-color="#13ce66" inactive-color="#ff4949">
					</el-switch>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间">
			</el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('creditScore:update')"
						@click="amendBanner(scope.row)">修改
					</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('creditScore:delete')"
						@click="deletes(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 50, 100]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="tableData.total">
			</el-pagination>
		</div>
		<el-dialog :title="title" :visible.sync="dialogFormVisible" center>
			
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">名称：</span>
				<el-input style="width:50%;" v-model="levelName" placeholder="请输入名称"></el-input>
			</div>
			<div style="margin-bottom: 10px;display: flex;">
				<span style="width: 200px;display: inline-block;text-align: right;">图片：</span>
				<div style=" width:148px;height:148px;border: 1px dashed #c0ccda;border-radius: 6px;text-align: center;line-height: 148px;">
					<el-upload
						class="avatar-uploader"
						v-model="iconImg"
						:action="$http.adornUrl('alioss/upload')"
						:show-file-list="false"
						:on-success="handleAvatarSuccess"
						>
						<img v-if="iconImg" :src="iconImg" class="avatar" style="border-radius: 6px;width: 148px;height: 148px;"/>
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</div>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">分佣比例：</span>
				<el-input style="width:50%;" v-model="scale" placeholder="请输入分佣比例"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">完成单数：</span>
				<el-input style="width:50%;" v-model="orderCount" placeholder="请输入完成单数"></el-input>
			</div>
			<!-- <div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">排序：</span>
				<el-input style="width:50%;" v-model="sort" placeholder="请输入排序"
					onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"></el-input>
			</div> -->
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 10,
				page1: 1,
				size1: 10,
				tableDataLoading: true,
				dialogFormVisible: false,
				tableData: [],
				scale: '',
				levelName:'',
				iconImg:'',
				orderCount: '',
				isEnable: 1,
				sort: '',
				rewardId: '',
				title: '添加',
				openValue: 1,
				closeValue: 0,
			}
		},
		methods: {
			// 获取数据列表
			dataSelect() {
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/rewardLevel/getLevelList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit
					})
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log('会员列表', data)
						this.tableDataLoading = false
						let returnData = data.data;
						this.tableData = returnData;
					}

				})
			},
			Notice() {
				this.page = 1
				this.dataSelect()
			},
			addNotice() {
				this.orderCount = ''
				this.scale = ''
				this.levelName = ''
				this.isEnable = 1
				this.sort = ''
				this.rewardId = ''
				this.iconImg = ''
				this.title = '添加'
				this.dialogFormVisible = true
			},
			handleCurrentChange(val) {
				this.page = val
				this.dataSelect()
			},
			handleSizeChange(val) {
				this.limit = val
				this.dataSelect()
			},
			//上传成功
			handleAvatarSuccess(file) {
				this.iconImg = file.data
			},
			// 添加banner图
			addNoticeTo() {
				if (this.levelName == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入名称',
						type: 'warning'
					});
					return
				}
				if (this.iconImg == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请上传图标',
						type: 'warning'
					});
					return
				}
				if (this.scale == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入分佣比例',
						type: 'warning'
					});
					return
				}
					if (this.orderCount === '') {
						this.$notify({
							title: '提示',
							duration: 1800,
							message: '请输入完成单数',
							type: 'warning'
						});
						return
					}
					
				if (this.title == '添加') {
					var url = 'admin/rewardLevel/saveLevel'
				} else {
					var url = 'admin/rewardLevel/saveLevel'
				}
				this.$http({
					url: this.$http.adornUrl(url),
					method: 'post',
					data: this.$http.adornData({
						'orderCount': this.orderCount,
						'scale': this.scale,
						'levelName':this.levelName,
						'iconImg': this.iconImg,
						'isEnable': this.isEnable,
						'rewardId': this.rewardId,
					},false,'from')
				}).then(({
					data
				}) => {
					if (data.code == 0) {
						this.dialogFormVisible = false
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					} else {
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}

				})
			},
			// 修改
			amendBanner(rows) {
				this.dialogFormVisible = true
				this.rewardId = rows.rewardId
				this.orderCount = rows.orderCount
				// this.sort = rows.sort
				this.isEnable = rows.isEnable
				this.scale = rows.scale
				this.levelName = rows.levelName
				this.iconImg = rows.iconImg
				this.title = '修改'
			},
			//删除一级
			deletes(row) {
				let delid = row.rewardId
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl(
							`admin/rewardLevel/deleteLevelById?rewardId=${delid}`),
						method: 'post',
						params: this.$http.adornParams({})
					}).then(({
						data
					}) => {
						if(data.code==0){
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.dataSelect()
								}
							})
						}else {
						this.$message({
							message: data.msg,
							type: 'error',
							duration: 1500,
							onClose: () => {
							}
						})
					}
						
					})
				}).catch(() => {})
			},
			// 启用与否
			change(row) {
				this.$http({
					url: this.$http.adornUrl('admin/rewardLevel/saveLevel'),
					method: 'post',
					data: this.$http.adornData({
						// 'orderCount': row.orderCount,
						// 'scale': row.scale,
						// 'sort': row.sort,
						'isEnable': row.isEnable,
						'rewardId': row.rewardId,
					},false,'from')
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}else {
					this.$message({
						message: data.msg,
						type: 'error',
						duration: 1500,
						onClose: () => {
						}
					})
				}
				})
			},
		},
		mounted() {
			this.dataSelect()
		}
	}
</script>

<style>
</style>
